package com.idp.web.service.impl;

import com.idp.common.base.BaseResult;
import com.idp.common.base.BusinessException;
import com.idp.common.constants.CommonConstants;
import com.idp.common.constants.Constants;
import com.idp.common.persistence.Page;
import com.idp.web.dao.HospitalDepartmentDao;
import com.idp.web.dao.NurseDao;
import com.idp.web.dto.NurseDTO;
import com.idp.web.dto.NurseListDTO;
import com.idp.web.dto.NurseShowDTO;
import com.idp.web.entity.Hospitals;
import com.idp.web.entity.Nurse;
import com.idp.web.service.INurseService;
import com.idp.web.system.dao.SysUserDao;
import com.idp.web.system.entity.SysUser;
import com.idp.web.system.service.SysUserService;
import com.idp.web.vo.NurseSearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 护士service实现类
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/3/1
 */
@Service("nurseService")
public class NurseServiceImpl implements INurseService {

    @Resource
    private NurseDao nurseDao;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private HospitalDepartmentDao hospitalDepartmentDao;

    /**
     * 根据条件分页查询医生列表
     *
     * @param nurseSearchVo 查询信息
     * @param page  分页信息
     * @return Page<NurseListDTO>
     */
    @Override
    public Page<NurseListDTO> findByPage(NurseSearchVo nurseSearchVo, Page<NurseListDTO> page) {
        page.setResult(nurseDao.selectPage(page,nurseSearchVo));
        return page;
    }

    /**
     * 根据条件查询医生列表
     *
     * @param nurse 查询条件
     * @return List<Nurse>
     */
    @Override
    public List<Nurse> findBySearch(Nurse nurse) {
        return nurseDao.find(nurse);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键id
     * @return Nurse
     */
    @Override
    public Nurse getById(String id) {
        if (null == id){
            return null;
        }
        return nurseDao.getById(id);
    }

    /**
     * 通过ID查询护士信息
     *
     * @param id 主键id
     * @return NurseShowDTO
     */
    @Override
    public NurseShowDTO getNurseById(String id) {
        if (null == id){
            return null;
        }
        return nurseDao.getNurseById(id);
    }

    /**
     * 编辑页面初始化
     *
     * @param id 主键id
     * @return NurseDTO
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public NurseDTO getNurseForEdit(String id) {
        if (null == id){
            return null;
        }
        return nurseDao.getNurseForEdit(id);
    }

    /**
     * 新增
     *
     * @param nurse 护士信息
     * @param userType 账户类型：1、管理员,2、医院，3、护士，4、医生
     * @param userId 用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult add(Nurse nurse,Integer userType, String userId) {
        //根据手机号验证是否已经被注册过
        Integer phoneNum = nurseDao.getCountByPhone(nurse.getPhone());
        if (null == phoneNum || phoneNum > CommonConstants.INT_ZERO){
            return new BaseResult(Constants.RESPONSE_CODE_601,Constants.RESPONSE_MESSAGE_601);
        }

        nurse.preInsert(userId);
        nurse.setAccountStastus(CommonConstants.NURSE_STATUS_WAIT);

        //医院信息
        if (CommonConstants.USER_TYPE_HOSPITAL == userType){
            Hospitals hospitals = hospitalDepartmentDao.selectHospital(userId,nurse.getDepartmentId());
            if (null != hospitals){
                nurse.setHospitalId(hospitals.getId());
                nurse.setHospitalName(hospitals.getHospitalName());
            }
        }

        Integer changeNum = nurseDao.add(nurse);
        if (null == changeNum || changeNum < CommonConstants.INT_ONE){
            return new BaseResult(Constants.RESPONSE_CODE_605,Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = new SysUser();
        BeanUtils.copyProperties(nurse,user);
        user.setUsername(nurse.getUserName());
        user.setUserType(CommonConstants.USER_TYPE_NURSE);
        sysUserService.registerAdd(user);

        return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);

    }

    /**
     * 修改
     *
     * @param nurse 护士信息
     * @param userId 用户id
     * @param userType 账户类型：1、管理员,2、医院，3、护士，4、医生
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult update(Nurse nurse, String userId,int userType) {
        //根据手机号验证是否已经被注册过
        Nurse oldNurse = nurseDao.getById(nurse.getId());
        Integer phoneNum = nurseDao.getCountByPhone(nurse.getPhone());
        if (null == phoneNum || (phoneNum > CommonConstants.INT_ZERO && !nurse.getPhone().trim().equals(oldNurse.getPhone()))){
            return new BaseResult(Constants.RESPONSE_CODE_601,Constants.RESPONSE_MESSAGE_601);
        }
        nurse.preUpdate(userId);

        //医院信息
        if (CommonConstants.USER_TYPE_HOSPITAL == userType){
            Hospitals hospitals = hospitalDepartmentDao.selectHospital(userId,nurse.getDepartmentId());
            if (null != hospitals){
                nurse.setHospitalId(hospitals.getId());
                nurse.setHospitalName(hospitals.getHospitalName());
            }
        }

        //管理员登录
        if (CommonConstants.USER_TYPE_ADMIN == userType){
            nurse.setHospitalId(oldNurse.getHospitalId());
            nurse.setHospitalName(oldNurse.getHospitalName());
        }

        nurse.setAccountStastus(oldNurse.getAccountStastus());
        nurse.setCreateUser(oldNurse.getCreateUser());
        nurse.setCreateDate(oldNurse.getCreateDate());
        nurse.setLogicDelete(oldNurse.getLogicDelete());
        Integer changeNum = nurseDao.update(nurse);
        if (null == changeNum || changeNum < CommonConstants.INT_ZERO){
            return new BaseResult(Constants.RESPONSE_CODE_605,Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = sysUserService.getById(nurse.getId());
        BeanUtils.copyProperties(nurse,user);
        sysUserService.update(user);

        return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 审核
     *
     * @param nurse 护士信息
     * @param userId 用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult audit(Nurse nurse, String userId){
        //验证
        if (null == nurse || StringUtils.isEmpty(nurse.getId()) || null == nurse.getAccountStastus()){
            return new BaseResult(Constants.RESPONSE_CODE_400,"参数错误");
        }
        Integer accountStatus = nurse.getAccountStastus();
        if (CommonConstants.NURSE_STATUS_SUCCESS != accountStatus && CommonConstants.NURSE_STATUS_FAIL != accountStatus){
            return new BaseResult(Constants.RESPONSE_CODE_400,"参数错误");
        }
        Nurse nurse1 = getById(nurse.getId().trim());
        if (null == nurse1){
            return new BaseResult(Constants.RESPONSE_CODE_602,Constants.RESPONSE_MESSAGE_602);
        }
        nurse1.preUpdate(userId);
        nurse1.setAccountStastus(nurse.getAccountStastus());

        nurse1.setAuditUser(userId);
        nurse1.setAuditDate(new Date());

        Integer changeNum = nurseDao.update(nurse1);
        if (null == changeNum || changeNum < CommonConstants.INT_ONE){
            return new BaseResult(Constants.RESPONSE_CODE_606,Constants.RESPONSE_MESSAGE_606);
        }
        return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 根据主键id删除护士信息
     *
     * @param id 护士id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void delete(String id) {
        //护士信息表删除
        nurseDao.delete(id);
        //账户信息表删除
        sysUserService.delete(id);
    }

    /**
     * 查询护士的统计信息
     *
     * @param nurseSearchVo 查询条件
     * @return int
     */
    @Override
    public int queryStatistics(NurseSearchVo nurseSearchVo){
        int queryNum = 0;
        Integer selectNum = nurseDao.queryStatistics(nurseSearchVo);
        if (null == selectNum || selectNum < CommonConstants.INT_ZERO){
            queryNum = CommonConstants.INT_ZERO;
        }else{
            queryNum = selectNum;
        }
        return queryNum;
    }

}